// pages/center/invoice/index/index.js
const app = getApp();
const service = require('../../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 状态
    navIdx: 1,
    // 页码
    page: 0,
    // 显示的列表
    list: [],
    // 列表数据
    applyList: [],
    // 已申请列表
    hadApplyList: [],
    // 是否选择全部
    all: {
      id: 'ALL',
      payMoney: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.getOrderCanApplyInvoice();
    options.pageName && wx.setNavigationBarTitle({
      title: options.pageName,
    });
  },

  // 跳转微信发票插件
  toWechatInvoiceHeader() {
    wx.chooseInvoiceTitle({
      success(res) {
        console.log(res);
      }
    })
  },

  // 菜单选择
  bindTapNav(e) {
    this.setData({
      page: 0,
      // 列表数据
      applyList: [],
      // 已申请列表
      hadApplyList: []
    })
    if (e.currentTarget.dataset.idx !== this.data.navIdx) {
      if (e.currentTarget.dataset.idx === 1) {
        this.getOrderCanApplyInvoice();
      } else {
        this.getOrderHadApplyInvoice();
      }
    }
    this.setData({
      navIdx: e.currentTarget.dataset.idx
    })
  },

  // 获取可开票订单列表数
  getOrderCanApplyInvoice() {
    var that = this;
    var params = {
      page: this.data.page + 1,
    }
    service.getConnect(service.api.getOrderCanApplyInvoice, params).then(function(res) {
      wx.hideLoading();
      that.setData({
        list: that.data.applyList.concat(res.data.list),
        applyList: that.data.applyList.concat(res.data.list),
        ['all.payMoney']: res.data.totalMoney
      });
    })
  },

  // 获取已开票订单列表数
  getOrderHadApplyInvoice() {
    var that = this;
    var params = {
      page: this.data.page + 1,
    }
    service.getConnect(service.api.getOrderHadApplyInvoice, params).then(function(res) {
      wx.hideLoading();
      that.setData({
        page: that.data.page + 1,
        list: that.data.hadApplyList.concat(res.data.list),
        hadApplyList: that.data.hadApplyList.concat(res.data.list)
      });
    })
  },

  // 跳转开票详情
  bintToAppleInvoice(e) {
    console.log(e);
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../apply/apply?id=' + item.id+'&payMoney='+item.payMoney+'&type='+ this.data.navIdx,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      page: 0,
      // 列表数据
      applyList: [],
      // 已申请列表
      hadApplyList: []
    })
    if (this.data.navIdx === 1) {
      this.getOrderCanApplyInvoice();
    } else {
      this.getOrderHadApplyInvoice();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 0,
      // 列表数据
      applyList: [],
      // 已申请列表
      hadApplyList: []
    })
    if (this.data.navIdx === 1) {
      this.getOrderCanApplyInvoice();
    } else {
      this.getOrderHadApplyInvoice();
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.navIdx === 1) {
      this.getOrderCanApplyInvoice();
    } else {
      this.getOrderHadApplyInvoice();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let wxappStyle = wx.getStorageSync('wxappStyle');
    return {
      title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
      path: '/pages/index/index'
    }

  }
})
